package com.mall.manager.controller;

import java.io.File;
import java.io.FileInputStream;
import java.nio.channels.FileChannel;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.manager.service.ScheduleJobService;
import com.mall.untils.DateUtils;
import com.mall.untils.PropertiesUtil;
import com.mall.untils.UploadImgOrRecorderUtil;

/**
 * 备份文件到oss类
 * @author tgy
 *
 */
@RequestMapping("/manager")
@Controller
public class ManagerController {

	public Logger logger = Logger.getLogger(ManagerController.class);
	
	@Resource
	private ScheduleJobService scheduleJobService;
	
	/**
	 * 备份文件到阿里服务器，存储的文件都是Object(包括文件夹:0size的文件)
	 * @return url
	 */
	@RequestMapping("/uploadOss")
	@ResponseBody
	public String uploadFileToOss(){
		String url = null;
		try{
			String filePath = PropertiesUtil.getProperties("fileLocalPath")+DateUtils.getNowBefor()+".log";
			File file = new File(filePath);
			if(file.exists()){
				//上传到阿里服务器
				FileInputStream fi = new FileInputStream(file);
				FileChannel fc = fi.getChannel();
				url = UploadImgOrRecorderUtil.createImgFileByName(fi, fc.size(), "data_platform/txt/"+DateUtils.getNowBefor()+".txt");
			}else{
			}
		}catch(Exception e){
			logger.info("备份文件到阿里服务器出现异常："+e.getMessage());
		}
		return url;
	}
}
